package com.entor.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entor.dao.DuesDaoI;
import com.entor.dao.PartymemberDaoI;
import com.entor.model.Dues;
import com.entor.model.Msg;
import com.entor.model.Partymember;
import com.entor.util.SendUtil;



@Service
public class DuesServiceI implements BaseServiceI<Dues>{

	@Autowired
	private DuesDaoI ddi;
	
	@Autowired
	private PartymemberDaoI pms;


	public List<Dues> find(Dues t) {
		
		return ddi.find(t);
	}

	public Dues get(Dues t) {
		List<Dues> list = ddi.find(t);
		return list.size()>0?list.get(0):new Dues();
		
	}

	public Msg insert(Dues t) {
		
		int i = ddi.insert(t);
		Msg m = new Msg("新增失败",false);
		if(i>0){
			m =new Msg("新增成功",true);
		}
		
		return m;
		
		
	}
	
	public Msg send(Partymember p) {
		
		Msg m = new Msg("催缴失败",false);
		String email = p.getEmail();
		String content = "请您尽快缴纳党费";
		String title = "党费催缴";
		int  i = Integer.parseInt(p.getGrade());
		Dues t = new Dues();
		t.setId(i);
		t = this.get(t);
		int owe = t.getOwe();
		if(owe==0){
			
			m = new Msg("无法催缴，该党员以缴清党费！",false);
			return m;
		}
		boolean flat = SendUtil.sendEmail(email, content,title);
		
		if(flat){
			m =new Msg("催缴成功",true);
		}
		
		return m;
		
		
	}
	

	public Msg batchInsert(String objs) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg edit(Dues t) {
		
		if(t.getPay()==null) t.setPay(0);
		
		
		int owe = t.getOwe()-t.getNewpay();
		int pay = t.getNewpay()+t.getPay();
		t.setOwe(owe);
		t.setPay(pay);
		
		int i = ddi.edit(t);
		Msg m = new Msg("缴费失败",false);
		if(i>0){
			m =new Msg("缴费成功",true);
		}
		
		return m;
	}

	public Msg remove(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg batchRemove(String ids) {
		
		String rids[] = ids.split(",");
		List<String> list = new ArrayList<String>();
		list = Arrays.asList(rids);
		for (String s : list) {
			Partymember p =new Partymember(); 
			p.setGrade(s);
			List<Partymember> li = pms.find(p);
			p =  li.size()>0?li.get(0):new Partymember();
			String email = p.getEmail();
			
			String content = "请您尽快缴纳党费";
			String title = "党费催缴";
			boolean flat = SendUtil.sendEmail(email, content,title);
		}
		
		Msg m = new Msg("催缴成功!",true);
		return m;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
